package cn.jxau.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.service.ArticleTypeService;

@Service
public class ArticleTypeServiceImpl extends AbstractCommonService implements ArticleTypeService {

	@Override
	public List<ArticleType> getAllArticleType() {
		return articleTypeMapper.selectList(null);
	}

	@Override
	public int updateTypeNameById(ArticleType articleType) {
		// TODO Auto-generated method stub
		return articleTypeMapper.updateById(articleType);
	}

	/**
	 * 递归删除父节点以及所有子节点
	 * */
	@Override
	public int deleteTypeParentById(Long id,boolean isParent) {
		int returnFlag=1;
		ArticleType type = articleTypeMapper.selectById(id);
		List<ArticleType> list = getAllArticleType();
		try {
			if(isParent){
				for(int i=0;i<list.size();i++){
					if(id.equals(list.get(i).getPid())){
						deleteTypeParentById(list.get(i).getId(), type.getHasItem());
					}
				}
				articleTypeMapper.deleteById(id);
			}else{
				deleteTypeSonById(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnFlag=0;
		}
		
		return returnFlag;
	}

	
	/**
	 * 删除父节点的最后一个节点后 父节点变子节点
	 * 
	 * @author lpy
	 
	@Override
	public int deleteSonById(Long id) {
		int returnFlag=1;
		ArticleType type = articleTypeMapper.selectById(id);
		List<ArticleType> list = getAllArticle();
		try {
			articleTypeMapper.deleteById(id);
			boolean flag=true;
			for(int i=0;i<list.size();i++){
				if(type.getPid().equals(list.get(i).getPid())){
					flag = false;
					break;
				}
			}
			if(flag){
				ArticleType entity = new ArticleType();
				entity.setId(type.getPid());
				entity.setHasItem(false);
				articleTypeMapper.updateById(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnFlag=0;
		}
		
		return returnFlag;
	}
	* */
	
	@Override
	public int deleteTypeSonById(Long id) {
		return articleTypeMapper.deleteById(id);
	}

	/**
	 * @author lpy
	 * 
	 * @return当前插入文章的ID
	 * */
	@Override
	public Long insertType(ArticleType articleType) {
		ArticleType type = null;
		ArticleType parent = null;
		ArticleType entity = null;
		if(articleTypeMapper.insert(articleType)>0){
			//此处没有做null 判断 因为如果插入成功 查询则不会为空 如果出错 可以优先检查这里
			type = articleTypeMapper.selectOne(articleType);
			//根结点为虚拟 跳过根节点
			if(type.getPid()!=1){
				parent = articleTypeMapper.selectById(type.getPid());
				//判断该节点是否是子节点 
				if(!parent.getHasItem()){
					entity = new ArticleType();
					entity.setId(type.getPid());
					entity.setHasItem(true);
					if(articleTypeMapper.updateById(entity)>0){
						return (Long)type.getId();
					}
				}
			}
		}
		return (Long)type.getId();
	}

	@Override
	public int insertDragType(Long id, ArticleType articleType) {
		int returnFlag=1;
		ArticleType type = null;
		ArticleType drag = null;
		ArticleType entity = null;
		try {
			drag = articleTypeMapper.selectById(id);
			type = new ArticleType();
			type.setId(id);
			type.setPid(articleType.getId());
			if(drag.getHasItem()){
				type.setHasItem(true);
			}
			articleTypeMapper.updateById(type);
			if(!articleType.getHasItem()){
				entity = new ArticleType();
				entity.setId(articleType.getId());
				entity.setHasItem(true);
				articleTypeMapper.updateById(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnFlag=0;
		}
		return returnFlag;
	}

	@Override
	public Map<String, ArticleType> initParentAndSon() {
		Map<String, ArticleType> map = new HashMap<String, ArticleType>();
		ArticleType parent = articleTypeMapper.initParent();
		ArticleType son = articleTypeMapper.initSon(parent.getId());
		map.put("parent", parent);
		map.put("son", son);
		return map;
	}
}












